package com.mbc.hr.manager;

import java.util.List;

import javax.persistence.TemporalType;

import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.DateUtils;
import com.mbc.hr.data.EmployeeAttendancePenaltyDataList;
import com.mbc.hr.entities.EmployeeAttendancePenalty;
import com.mbc.hr.param.EmployeeAttendancePenaltyCriteria;

public class EmployeeAttendancePenaltyMgr extends AbstractMgr {
	
	public EmployeeAttendancePenaltyMgr(){}
	
	public EmployeeAttendancePenaltyMgr(String sessionID){
		this.sessionId = sessionID;
	}
		
	@SuppressWarnings("unchecked")
	public EmployeeAttendancePenaltyDataList filterEmployeeAttendancePenalty(EmployeeAttendancePenaltyCriteria cri) throws Exception {
		openConnection();
		try {
            String l_cri = " WHERE (eap.employeeID = " + cri.getEmployeeID() + " or " + cri.getEmployeeID() + "=0)" + 
            				" AND eap.datetime_ >= :startDate AND eap.datetime_ <= :endDate " + 
            				" AND eap.status = 1 " + 
            				" AND (eap.penaltyType = " + cri.getPenaltyType() + " or " + cri.getPenaltyType() + "=0)" +  
            				" AND eap.minutes >= :startMinutes AND eap.minutes <= :endMinutes";
            
            String l_query = "SELECT eap FROM EmployeeAttendancePenalty as eap" + l_cri;
            List<EmployeeAttendancePenalty> l_results = entityManager.createQuery(l_query)
            		.setParameter("startDate", cri.getStartdatetime_(), TemporalType.DATE)		// cri.getStartdatetime_()
            		.setParameter("endDate", cri.getEnddatetime_(), TemporalType.DATE)	// cri.getEnddatetime_()
            		.setParameter("startMinutes", cri.getStartminutes())
            		.setParameter("endMinutes", cri.getEndminutes())
                    .setFirstResult(cri.getIndex())
                    .setMaxResults(cri.getSize())
                    .getResultList();
            
            EmployeeAttendancePenaltyDataList l_dataList = new EmployeeAttendancePenaltyDataList();
            l_dataList.setEntityList(l_results);
            
            if(cri.getIndex() == 0){
            	l_query = "SELECT count(eap.penaltyID) FROM EmployeeAttendancePenalty as eap" + l_cri;
            	l_dataList.setTotalRecord(Long.parseLong(entityManager.createQuery(l_query)
            			.setParameter("startDate", DateUtils.convert("2015-01-01",  DateUtils.DATE_YYYYMMDD_FORMAT), TemporalType.DATE)	// cri.getStartdatetime_()
                		.setParameter("endDate", DateUtils.getSysdateWithoutTime(), TemporalType.DATE)	// cri.getEnddatetime_()
                		.setParameter("startMinutes", cri.getStartminutes())
                		.setParameter("endMinutes", cri.getEndminutes())
                		.getSingleResult().toString()));
            }
            
			return l_dataList;
		} catch (Exception e) {
			throw e;
		} finally{
			closeConnection();
		}
//		openConnection();
//		try{
//			return entityManager.createNamedQuery("EmployeeAttendancePenalty.dataFilterQuery", EmployeeAttendancePenalty.class)
//					.setParameter("employeeId", criteria.getIndex()) 
//					.setParameter("startDateTime", (startDateTime != null)? startDateTime : null)
//					.setParameter("endDateTime", (endDateTime != null)? endDateTime : null)
//					.setParameter("penaltyType", (penaltyType != null)? penaltyType : null) 
//					.getResultList();
//		}catch(Exception e) {
//			throw e;
//		}finally {
//			closeConnection();
//		}
	}
	
}
